package com.example.GeoQuiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity
{
  private Button mTrueButton;
  private Button mFalseButton;
  private ImageButton mNextButton;
  private ImageButton mPrevButton;
  private TextView mQuestionTextView;
  private View.OnClickListener onClickListener;
  private View.OnClickListener prevClickListener;

  final private TrueFalse[] mQuestionBank = new TrueFalse[]
    {
      new TrueFalse( R.string.question_oceans, true ),
      new TrueFalse( R.string.question_mideast, false ),
      new TrueFalse( R.string.question_africa, false ),
      new TrueFalse( R.string.question_americas, true ),
      new TrueFalse( R.string.question_asia, true )
    };

  private int mCurrentIndex = 0;

  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_quiz );

    mQuestionTextView = (TextView) findViewById( R.id.question_text_view );
    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText( question );
    mQuestionTextView.setOnClickListener( getOnClickListener() );

    mTrueButton = (Button) findViewById( R.id.true_button );
    mTrueButton.setOnClickListener( new View.OnClickListener()
    {
      public void onClick( View view )
      {
        checkAnswer( true );
      }
    } );
    mFalseButton = (Button) findViewById( R.id.false_button );
    mFalseButton.setOnClickListener( new View.OnClickListener()
    {
      public void onClick( View view )
      {
        checkAnswer( false );
      }
    } );

    mNextButton = (ImageButton) findViewById( R.id.next_button );
    mNextButton.setOnClickListener( getOnClickListener() );
    mPrevButton = (ImageButton) findViewById( R.id.previous_button );
    mPrevButton.setOnClickListener( getPrevClickListener() );
  }

  private void updateQuestion()
  {
    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText( question );
  }

  private void checkAnswer( final boolean userInput )
  {
    boolean ansIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

    int messageResId;

    if ( userInput == ansIsTrue )
    {
      messageResId = R.string.correct_toast;
    }
    else
    {
      messageResId = R.string.incorrect_toast;
    }

    Toast.makeText( this, messageResId, Toast.LENGTH_SHORT ).show();
  }

  private View.OnClickListener getOnClickListener()
  {
    if ( onClickListener == null )
    {
      onClickListener = new View.OnClickListener()
      {
        public void onClick( View view )
        {
          mCurrentIndex = ( mCurrentIndex + 1 ) % mQuestionBank.length;
          updateQuestion();
        }
      };
    }

    return onClickListener;
  }

  private View.OnClickListener getPrevClickListener()
  {
    if ( prevClickListener == null )
    {
      prevClickListener = new View.OnClickListener()
      {
        public void onClick( View view )
        {
          mCurrentIndex = ( ( mCurrentIndex + 4 ) % mQuestionBank.length );
          updateQuestion();
        }
      };
    }

    return prevClickListener;
  }
}
